import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Solution1 {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    //JZ32 从上往下打印二叉树
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        Queue<TreeNode> q1=new LinkedList <>();
        ArrayList<Integer> list=new ArrayList<>();
        if(root==null){
            return list;
        }
        q1.offer(root);
        TreeNode cur=root;
        while(!q1.isEmpty()){
            cur=q1.peek();
            if(cur.left!=null){
                q1.offer(cur.left);
            }
            if(cur.right!=null){
                q1.offer(cur.right);
            }
             TreeNode node=q1.poll();
            list.add(node.val);
            }
            return list;    
    }
}
